package com.atguigu.userconservice.controller;


import com.atguigu.commonutils.R;
import com.atguigu.commonutils.utils.JwtUtils;
import com.atguigu.servicebase.VO.LoginVo;
import com.atguigu.servicebase.VO.RegisterVo;
import com.atguigu.userconservice.entity.Author;
import com.atguigu.userconservice.service.AuthorService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author testjava
 * @since 2020-05-09
 */
@RestController
@RequestMapping("/userconservice/author")
@CrossOrigin
public class AuthorController {

    @Autowired
    private AuthorService authorService;

    @ApiOperation(value = "注册方法")
    @PostMapping("register")
    public R register(@RequestBody RegisterVo registerVo){
        authorService.registerInfo(registerVo);
        return R.ok();
    }

    @ApiOperation("登陆方法")
    @PostMapping("login")
    public R login(@RequestBody LoginVo loginVo){
        //返回token字符串中包含用户信息
        String token = authorService.logininfo(loginVo);
        return R.ok().data("token",token);
    }

    @ApiOperation("根据token获得用户信息")
    @PostMapping("getInfoToken")
    public R getInfoToken(HttpServletRequest request){
        //通过token字符串获得用户Id
        String memberId = JwtUtils.getMemberIdByJwtToken(request);
        //通过用户Id获取用户信息
        Author author = authorService.getAuthorInfo(memberId);
        return R.ok().data("author",author);
    }

    @PostMapping("addauthoeinfo")
    public R addauthoeinfo(@RequestBody Author author){
        authorService.addauthoeinfo(author);
        return R.ok();
    }
}